package tubesrus.beans;

import java.security.Security;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class MailManager {
	
	public MailManager() {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	}
	
	public boolean sendConfirmation(String name, String email) {
		
		String site = "http://localhost:8080/Tubesrus";
		String code = Md5.encode(name+"_confirmation");
		String lien = site+"/confirmation.jsp?id="+name+"&code="+code;
		
		String subject = "[Confirmation] Bienvenue sur Tubes R'US";
		String bodyText = 
			 "Vous êtes presque membre de la communauté Tubes R'Us !\n\n"
			+"Pour confirmer votre inscription, veuillez cliquer sur le lien suivant:\n"
			+lien+"\n\n"
			+"Ou entrez simplement votre code de confirmation: "+code+" à l'adresse suivante: "+site+"/confirmation.jsp?id="+name+"\n\n"
			+"Merci et à tout de suite sur Tubes R'Us !";
		
		return sendOne(email, subject, bodyText);
	}
	
	public boolean sendOne(String email, String subject, String bodyText) {
		ArrayList<String> to = new ArrayList<String>();
		to.add(email);
		return sendMany(to, subject, bodyText);
	}
	
	
    public boolean sendMany(ArrayList<String> to, String subject, String bodyText) {
    	final String username = "tubesrus.n7@gmail.com";
    	final String password = "tubesrus";
    	
        //String from = "Tubes R'Us";
        
        Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.socketFactory.fallback", "false");
		props.setProperty( "mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", "465");
		props.put("mail.smtp.starttls.enable","true");


        Session session = Session.getInstance(props,
			new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username, password);
				}
			});
 

        try {
        	session.setDebug(true);
        	
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            for(int i=0; i<to.size(); i++) {
            	message.addRecipient(Message.RecipientType.TO, new InternetAddress(to.get(i)));
            }
            message.setSubject(subject);
            message.setSentDate(new Date());
            message.setText(bodyText);

            Transport transport = session.getTransport("smtps");
            transport.connect("smtp.gmail.com", 465, username, password);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();

			System.out.println("done");
			return false;
            
        } catch (MessagingException e) {
            e.printStackTrace();
            return true;
        }
    }
}